﻿using CPPEI.Coolzon.Event.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.ConsoleApp
{
    /// <summary>
    /// Interaction logic for DisableInteractiveWindow.xaml
    /// </summary>
    public partial class DisableInteractiveWindow : Window
    {
        public DisableInteractiveWindow()
        {
            InitializeComponent();
        }
        public object o_lock = new object();
        private Thread _Thread;
        private bool _IsRunning = false;
        private long _LastShowTicks;
        private long _InteractiveCount;
        private bool _IsHasShowed = false;
        private ManualResetEvent _manualResetEvent = new ManualResetEvent(false);

        private void Run()
        {
            while (_IsRunning)
            {
                if(DateTime.Now.Ticks - _LastShowTicks > TimeSpan.FromSeconds(30).Ticks)
                {
                    lock (o_lock)
                    {
                        _InteractiveCount = 0;
                    }
                    _IsRunning = false;
                    _Thread = null;
                    if (_IsHasShowed)
                    {
                        _IsHasShowed = false;
                        this.Dispatcher.Invoke(() =>
                        {
                            this.Hide();
                        });
                    }
                }
                Thread.Sleep(100);
            }
            _manualResetEvent.Set();
        }

        public void Ref(string message, bool isShow)
        {
            lock (o_lock)
            {
                _InteractiveCount++;
                _LastShowTicks = DateTime.Now.Ticks;
                tbMessage.Text = message;
                if (!_IsRunning)
                {
                    _IsRunning = true;
                    _Thread = new Thread(Run);
                    _Thread.Start();
                }
            }
            if (isShow && !_IsHasShowed)
            {
                _IsHasShowed = true;
                this.ShowDialog();
            }
        }

        public void Release()
        {
            lock (o_lock)
            {
                if(_InteractiveCount > 0)
                {
                    _InteractiveCount--;
                }
            }
            if (_InteractiveCount == 0)
            {
                _IsRunning = false;
                _manualResetEvent.WaitOne();
                _Thread = null;
                _IsHasShowed = false;
                this.Hide();
            }
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
        }
    }
}
